﻿using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces;
using System.Net;

namespace MyPhotoIndex.Settings
{
    [Serializable]
    public class ProxySettings : IProxySettings
    {
        private bool m_useProxy = false;
        private string m_url;
        private int m_port = 8080;
        private string m_userName;
        private string m_password;

        #region IProxySettings Members

        public bool UseProxy
        {
            get { return m_useProxy; }
            set { m_useProxy = value; }
        }

        public string Url
        {
            get { return m_url; }
            set { m_url = value; }
        }

        public int Port
        {
            get { return m_port; }
            set { m_port = value; }
        }

        public string UserName
        {
            get { return m_userName; }
            set { m_userName = value; }
        }

        public string Password
        {
            get { return m_password; }
            set { m_password = value; }
        }

        public void RefreshProxySettings()
        {
            if (UseProxy == false)
            {
                WebRequest.DefaultWebProxy = null;
            }
            else
            {
                WebProxy webProxy = new WebProxy(Url, Port);

                if (string.IsNullOrEmpty(UserName) == false &&
                    string.IsNullOrEmpty(Password) == false)
                {
                    webProxy.Credentials = new NetworkCredential(UserName, Password);
                }

                WebRequest.DefaultWebProxy = webProxy;
            }
        }

        #endregion
    }
}
